package com.quantdo.apex.elasticjob.boot;

import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;

/**
 * @author chenkuan
 * @version v1.0
 * @desc
 * @date 2019/12/11 011 19:40
 */
public abstract class JobStarter implements Job {

	@Override
	public void onApplicationEvent(ApplicationEvent event) {
		if (event instanceof ApplicationStartedEvent) {
			JobManage jobManage = initContext(((ApplicationStartedEvent) event).getApplicationContext());
			start(jobManage);
		}
	}

	protected abstract void start(JobManage jobManage);

	private JobManage initContext(ApplicationContext context) {
		JobManage jobManage = context.getBean(JobManage.class);
		return jobManage;
	}
}
